package com.haierp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.model.PackageLevel;
import com.haierp.service.IPackageLevelService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.PackageLevelQueryVO;
import com.wangzhixuan.commons.utils.StringUtils;

/**
 * 
 * Title: PackageLevelController.java
 * Description: 
 *
 * @author jc
 * Apr 8, 2017
 *
 */
@Service("packageLevelController")
@Controller
@RequestMapping("/packageLevel")
public class PackageLevelController {

	@Autowired
	private IPackageLevelService iPackageLevelService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(PackageLevel packageLevel) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null == packageLevel.getId()) {
			if ((StringUtils.isNotBlank(packageLevel.getName())) 
					&& (StringUtils.isNotBlank(packageLevel.getPackageEn()))) {
				if (!checkUniqueness(packageLevel)) {
					result.buildData("相同包装规格，包装级别名称不可以重复").buildIsSuccess(false);
				} else {
					result.buildIsSuccess(iPackageLevelService.insert(packageLevel));
				}
			} else {
				result.buildData("包装规格名称和包装规格类别英文名称不可以为空").buildIsSuccess(false);
			}
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(PackageLevel packageLevel) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != packageLevel.getId()) {

			if ((StringUtils.isNotBlank(packageLevel.getName())) 
					&& (StringUtils.isNotBlank(packageLevel.getPackageEn()))) {
				if (!checkUniqueness(packageLevel)) {
					result.buildData("相同包装规格，包装级别名称不可以重复").buildIsSuccess(false);
				} else {
					result.buildIsSuccess(iPackageLevelService.updateSelectiveById(packageLevel));
				}
			} else {
				result.buildData("包装规格名称和包装规格类别英文名称不可以为空").buildIsSuccess(false);
			}
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(PackageLevel packageLevel) {
		JsonResult<String> result = new JsonResult<>();

		if (null != packageLevel.getId()) {
			result.buildIsSuccess(iPackageLevelService.deleteSelective(packageLevel));
		} else {
			result.buildData("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<PackageLevel> result = new JsonResult<>();
		
		return result.buildData(iPackageLevelService.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/queryPackageLevelList")
	@ResponseBody
	public Object queryPackageLevelList(PackageLevelQueryVO packageLevelQueryVO) {
		JsonPageResult<List<PackageLevel>> result = iPackageLevelService.queryPackageLevelList(packageLevelQueryVO);
		
		return result.buildIsSuccess(true);
	}
	
	/**
	 * check unique
	 * @param packageLevel
	 * @return
	 */
	private Boolean checkUniqueness(PackageLevel packageLevel) {

		PackageLevelQueryVO packageLevelQueryVO = new PackageLevelQueryVO();
		packageLevelQueryVO.setName(packageLevel.getName());
		packageLevelQueryVO.setPackageEn(packageLevel.getPackageEn());
		//Check unique
		Integer count = iPackageLevelService.queryPackageLevelsCount(packageLevelQueryVO);
		
		return ((null == count) || (0 == count));
	}
}
